//
//  UserProtocolViewController.m
//  DA_V
//
//  Created by 周季阳 on 15/4/22.
//  Copyright (c) 2015年 zhouhai. All rights reserved.
//

#import "UserProtocolViewController.h"
#import "InterFace.h"
#import "UIViewAdditions.h"
#import "YoungAllData.h"
#import "GMDCircleLoader.h"

@interface UserProtocolViewController ()<YoungDelegate,UIWebViewDelegate>

@end

@implementation UserProtocolViewController

#pragma mark - viewDidLoad
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [self colorWithHexString:@"#F5F5F5"];
    [self initNav];
    
    navTitle.text = @"用户协议";
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    //[rightBtn setTitle:@"返回" forState:UIControlStateNormal];
    rightBtn.frame = CGRectMake(0, SCREEN_HEIGHT-0.088*SCREEN_HEIGHT, SCREEN_WIDTH, 0.088*SCREEN_HEIGHT);
    //rightBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    //rightBtn.backgroundColor = [UIColor whiteColor];
    [rightBtn setBackgroundImage:[UIImage imageNamed:@"backBtn"] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(clickRightBtn) forControlEvents:UIControlEventTouchUpInside];
    //[rightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.view addSubview:rightBtn];
    
    [GMDCircleLoader setOnView:self.view withTitle:@"加载中···" animated:YES];
    
    YoungAllData *data = [[YoungAllData alloc]init];
    data.delegate = self;
    [data userProtocol];
    
}
#pragma mark - 用户协议回调
-(void)userProtocol:(NSDictionary *)sign{
    [GMDCircleLoader hideFromView:self.view animated:YES];
    if ([[sign objectForKey:@"status"] isEqualToString:@"success"]) {
        [self initUI:[sign objectForKey:@"data"]];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:[sign objectForKey:@"reason"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
}
#pragma mark - 用户协议回调失败
-(void)requistFail{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"网络异常,无法加载数据" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}

#pragma mark - initUI
-(void)initUI:(NSString *)data{
    UIView *backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, navView.bottom, SCREEN_WIDTH, SCREEN_HEIGHT*0.7)];
    backgroundView.backgroundColor = [UIColor whiteColor];
    backgroundView.userInteractionEnabled = YES;
    [self.view addSubview:backgroundView];
    
    UIWebView *webView = [[UIWebView alloc] init];
    webView.frame = CGRectMake(0, navView.bottom, SCREEN_WIDTH, SCREEN_HEIGHT-64-44);
    [self.view addSubview:webView];
    NSURL *url = [NSURL URLWithString:data];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webView loadRequest:request];
    webView.scalesPageToFit = YES;
    
}

#pragma mark - 返回上一个界面
-(void)clickRightBtn{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)clickBackBtn{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
